package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.service.WarehouseBillDetailService;
import com.wing.bill.service.WarehouseBillService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.*;
import com.wing.product.model.entity.*;
import com.wing.product.model.response.WarehouseOutboundRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WarehouseOutboundRecordServiceImpl extends ServiceImpl<WarehouseOutboundRecordDao, WarehouseOutboundRecord> implements WarehouseOutboundRecordService{

    @Autowired
    WarehouseOutboundRecordDetailDao warehouseOutboundRecordDetailDao;
    @Autowired
    WarehouseOutboundRecordDetailService warehouseOutboundRecordDetailService;
    @Autowired
    WarehouseProductInventoryDao warehouseProductInventoryDao;
    @Autowired
    WarehouseProductInventoryService warehouseProductInventoryService;
    @Autowired
    WarehouseBillService warehouseBillService;
    @Autowired
    WarehouseBillDetailService warehouseBillDetailService;
    @Autowired
    WarehouseOutboundOrderDetailService warehouseOutboundOrderDetailService;
    @Autowired
    WarehouseOutboundOrderDetailDao warehouseOutboundOrderDetailDao;
    @Autowired
    WarehouseOutboundOrderService warehouseOutboundOrderService;

    @Override
    public Page<WarehouseOutboundRecordVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseOutboundRecordVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public  WarehouseOutboundRecordVO selectRecordDetail(String id){
        //查询出库记录
        WarehouseOutboundRecordVO warehouseOutboundRecordVO = this.baseMapper.selectRecordById(id);

        //查询出库记录明细
        LambdaQueryWrapper<WarehouseOutboundRecordDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WarehouseOutboundRecordDetail::getWarehouseOutboundRecordId,warehouseOutboundRecordVO.getId())
                .eq(WarehouseOutboundRecordDetail::getIsDeleted,0);
        List<WarehouseOutboundRecordDetail> warehouseOutboundOrderDetailList = warehouseOutboundRecordDetailDao.selectList(lambdaQueryWrapper);
        warehouseOutboundRecordVO.setWarehouseOutboundRecordDetailList(warehouseOutboundOrderDetailList);
        return warehouseOutboundRecordVO;
    }

    @Override
    @Transactional
    public JsonResult saveWarehouseOutboundRecord(WarehouseOutboundRecordVO warehouseOutboundRecordVO){

        //添加出库记录
        if (ObjectUtil.isNotEmpty(warehouseOutboundRecordVO)){
            WarehouseOutboundRecord warehouseOutboundRecord = new WarehouseOutboundRecord();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseOutboundRecord.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseOutboundRecordVO, warehouseOutboundRecord, copyOptions);
            warehouseOutboundRecord.setWarehouseOutboundRecordCode(UniqueIdGeneratorUtil.generateUniqueId("CKJL", "yyyyMMddHHmmss", 1000, 9999));
            warehouseOutboundRecord.setStatus(3);
            this.baseMapper.insert(warehouseOutboundRecord);
            //添加出库记录明细
            List<String> sysCodeList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(warehouseOutboundRecordVO.getWarehouseOutboundRecordDetailList())){
                for (WarehouseOutboundRecordDetail rd : warehouseOutboundRecordVO.getWarehouseOutboundRecordDetailList()){
                    rd.setWarehouseOutboundRecordId(warehouseOutboundRecord.getId());
                    sysCodeList.add(rd.getUserBundleCode());
                }
                warehouseOutboundRecordDetailService.saveBatch(warehouseOutboundRecordVO.getWarehouseOutboundRecordDetailList());
                if (CollectionUtils.isNotEmpty(sysCodeList)){
                    List<Long> inventoryIds = new ArrayList<>();
                    LambdaQueryWrapper<WarehouseProductInventory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.in(WarehouseProductInventory::getUserBundleCode,sysCodeList)
                            .eq(WarehouseProductInventory::getIsDeleted,0);
                    List<WarehouseProductInventory> warehouseProductInventories = warehouseProductInventoryDao.selectList(lambdaQueryWrapper);
                    for (WarehouseProductInventory wpi : warehouseProductInventories){
                        wpi.setProductStatus(4);
                        inventoryIds.add(wpi.getId());
                    }
                    warehouseProductInventoryService.updateBatchById(warehouseProductInventories);

                    //根据产品明细修改出库订单状态
                    LambdaUpdateWrapper<WarehouseOutboundOrderDetail> detailLambdaQueryWrapper = new LambdaUpdateWrapper<>();
                    detailLambdaQueryWrapper.eq(WarehouseOutboundOrderDetail::getIsDeleted,0)
                            .in(WarehouseOutboundOrderDetail::getProductId,inventoryIds)
                            .set(WarehouseOutboundOrderDetail::getStatus,2);
                    warehouseOutboundOrderDetailService.update(detailLambdaQueryWrapper);

                    LambdaQueryWrapper<WarehouseOutboundOrderDetail> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(WarehouseOutboundOrderDetail::getWarehouseOutboundOrderId,warehouseOutboundRecordVO.getWarehouseOutboundOrderId())
                            .eq(WarehouseOutboundOrderDetail::getIsDeleted,0);
                    List<WarehouseOutboundOrderDetail> warehouseOutboundOrderDetailList = warehouseOutboundOrderDetailDao.selectList(lambdaQueryWrapper1);

                    Boolean b = true;
                    for (WarehouseOutboundOrderDetail wo : warehouseOutboundOrderDetailList){
                        if (wo.getStatus() == 1){
                            b = false;
                        }
                    }
                    if (b){
                        LambdaUpdateWrapper<WarehouseOutboundOrder> orderLambdaQueryWrapper = new LambdaUpdateWrapper<>();
                        orderLambdaQueryWrapper.eq(WarehouseOutboundOrder::getId,warehouseOutboundRecordVO.getWarehouseOutboundOrderId())
                                        .set(WarehouseOutboundOrder::getStatus,5);
                        warehouseOutboundOrderService.update(orderLambdaQueryWrapper);
                    }

                    // 根据出库记录 修改账单信息
//                    warehouseBillDetailService.updateBillDetail(warehouseOutboundRecordVO.getCustomerId(),warehouseProductInventories.stream().map(WarehouseProductInventory::getSysBundleCode).collect(Collectors.toList()));
                }
            }
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.fail("添加失败");
        }

    }

    @Override
    public JsonResult deleteWarehouseOutboundRecord(Long id){
        if(ObjectUtil.isNotEmpty(id)){
            WarehouseOutboundRecord warehouseOutboundRecord = this.getById(id);
            warehouseOutboundRecord.setIsDeleted(true);
            this.updateById(warehouseOutboundRecord);
            return JsonResult.success("撤销成功");
        }else {
            return JsonResult.fail("id未空");
        }
    }
}
